package com.smartcommunity.controller.stastistics;

import com.smartcommunity.elasticsearch.StatisticEsApi;
import com.smartcommunity.bean.enumeration.CameraTypeEnum;
import com.smartcommunity.bean.model.Result;
import com.smartcommunity.entity.statistic.*;
import com.smartcommunity.service.businessCircle.RealCompanyService;
import com.smartcommunity.service.village.RealPowerOrgService;
import com.smartcommunity.service.village.impl.HouseService;
import com.smartcommunity.utils.DateUtils;
import com.smartcommunity.service.access.AccessDeviceService;
import com.smartcommunity.service.camera.CameraService;
import com.smartcommunity.service.people.PeopleService;
import com.smartcommunity.service.statistic.StatisticService;
import com.smartcommunity.service.vehilce.VehicleService;
import com.smartcommunity.service.village.VillageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author nieming
 * @descrition
 * @date 2020/6/4
 */
@Api(value = "大屏统计", tags = {"大屏统计"})
@RestController
@RequestMapping(value = "/statistics")
@Slf4j
public class StatisticsController {
    @Autowired
    private VillageService villageService;
    @Autowired
    private RealPowerOrgService realPowerOrgService;
    @Autowired
    private RealCompanyService realCompanyService;
    @Autowired
    private HouseService houseService;
    @Autowired
    private PeopleService peopleService;
    @Autowired
    private CameraService cameraService;
    @Autowired
    private AccessDeviceService accessDeviceService;
    @Autowired
    private VehicleService vehicleService;
    @Autowired
    private StatisticEsApi statisticEsApi;
    @Autowired
    private StatisticService statisticService;

    @ApiOperation(value = "获取社区资源数量", notes = "获取社区资源数量")
    @GetMapping(value = "/getResourceCounts")
    public Result getResourceCounts() {
        StatisticSmartCommunityCount statisticSmartCommunityCount = new StatisticSmartCommunityCount();
        //小区数量
        Integer villageCounts = villageService.getAllVillageCounts();
        statisticSmartCommunityCount.setVillageCounts(villageCounts);
        //实有力量
        Integer realPowerCounts = realPowerOrgService.getRealPowerCount();
        statisticSmartCommunityCount.setRealPowerCounts(realPowerCounts);
        //实有单位
        Integer realCompanyCount = realCompanyService.getRealCompanyCount();
        statisticSmartCommunityCount.setRealCompanyCount(realCompanyCount);
        //实有房屋
        Integer realHouseCount = houseService.getHouseCount();
        statisticSmartCommunityCount.setRealHouseCount(realHouseCount);
        //实有人口
        Integer realPeopleCopunt = peopleService.getAllPeopleCount();
        statisticSmartCommunityCount.setRealPeopleCount(realPeopleCopunt);
        //摄像机
        Integer normalCameraCount = cameraService.getCameraCountByType(CameraTypeEnum.normal.getValue());
        statisticSmartCommunityCount.setNormalCameraCount(normalCameraCount);
        //人脸相机
        Integer faceCameraCount = cameraService.getCameraCountByType(CameraTypeEnum.face.getValue());
        statisticSmartCommunityCount.setFaceCameraCount(faceCameraCount);
        //车辆相机
        Integer vehicleCameraCount = cameraService.getCameraCountByType(CameraTypeEnum.vehicle.getValue());
        statisticSmartCommunityCount.setVehicleCameraCount(vehicleCameraCount);
        //门禁设备
        Integer accessCount = accessDeviceService.getAccessDeviceCount();
        statisticSmartCommunityCount.setRealAcDeviceCount(accessCount);
        //实有设备
        Integer cameraCount = cameraService.getCameraCount();
        statisticSmartCommunityCount.setRealDeviceCount(cameraCount + accessCount);
        //实有车辆
        Integer vehicleCount = vehicleService.getVehicleCount();
        statisticSmartCommunityCount.setRealVehicleCount(vehicleCount);
        return Result.ok(statisticSmartCommunityCount);
    }

    @ApiOperation(value = "实有人口按类型统计", notes = "实有人口按类型统计")
    @GetMapping(value = "/getPeopleTypeCount")
    public Result<List<StatisticByTypeResp>> getPeopleTypeCount() {
        List<StatisticByTypeResp> statisticByTypeResps = peopleService.getPeopleTypeCount();
        return Result.ok(statisticByTypeResps);
    }

    @ApiOperation(value = "实有房屋按类型统计", notes = "实有房屋按类型统计")
    @GetMapping(value = "/getHouseTypeCount")
    public Result<List<StatisticByTypeResp>> getHouseTypeCount() {
        List<StatisticByTypeResp> statisticByTypeResps = houseService.getHouseTypeCount();
        return Result.ok(statisticByTypeResps);
    }

    @ApiOperation(value = "数据接入量统计", notes = "数据接入量统计")
    @GetMapping(value = "/getAccessDataCount")
    public Result<List<StatisticByTypeResp>> getAccessDataCount() {
        StatisticAccessData statisticAccessData = new StatisticAccessData();
        //接入数据总量
        statisticAccessData.setTotalAccessCount(statisticEsApi.getAllAccessCount());
        statisticAccessData.setTotalFaceCount(statisticEsApi.getAllFaceCount());
        statisticAccessData.setTotalVehicleCount(statisticEsApi.getAllVehicleCount());
        //今日接入量
        Long dayStart = DateUtils.getDayStart(new Date());
        Long dayEnd = DateUtils.getDayEnd(new Date());
        statisticAccessData.setTodayAccessCount(statisticEsApi.getAccessCountByTime(dayStart, dayEnd));
        statisticAccessData.setTodayFaceCount(statisticEsApi.getAllFaceCountByTime(dayStart, dayEnd));
        statisticAccessData.setTodayVehicleCount(statisticEsApi.getAllVehicleCountByTime(dayStart, dayEnd));
        return Result.ok(statisticAccessData);
    }

    @ApiOperation(value = "社区战法研判", notes = "社区战法研判")
    @ApiImplicitParam(name = "lastTime", value = "大于等于多少天未出现", example = "30", required = true)
    @GetMapping(value = "/getPeopleRegularStatistic")
    public Result<StatisticPeopleRegular> getPeopleRegularStatistic(@RequestParam(required = true, name = "lastTime") Integer lastTime) {
        StatisticPeopleRegular statisticAccessData = statisticService.getPeopleRegularStatistic(lastTime);
        return Result.ok(statisticAccessData);
    }

    @ApiOperation(value = "异常预警", notes = "异常预警")
    @GetMapping(value = "/geAlarmStatistic")
    public Result<List<StatisticExceptionAlarmResp>> geAlarmStatistic() {
        List<StatisticExceptionAlarmResp> result = statisticService.geAlarmStatistic();
        return Result.ok(result);
    }
    @ApiOperation(value = "社区流量统计", notes = "社区流量统计")
    @GetMapping(value = "/getFaceAccessStatistic")
    public Result<List<AccessStatisticDataResp>> getFaceAccessStatistic(@RequestParam String accessType) {
        List<AccessStatisticDataResp> result = statisticService.getFaceAccessStatistic(accessType);
        return Result.ok(result);
    }
    @ApiOperation(value = "小区地图信息", notes = "小区地图信息")
    @GetMapping(value = "/getStatisticsVillageList")
    public Result<List<StatisticsVillageDataResp>> getStatisticsVillageList(@RequestParam(required = false, name = "villageCode") String villageCode) {
        Map<String, Object> param = new HashMap<>(4);
        param.put("villageCode", villageCode);
        List<StatisticsVillageDataResp> result = villageService.getStatisticsVillageList(param);
        return Result.ok(result);
    }

}
